package com.lwc.cfdns.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwc.cfdns.common.Assert;
import com.lwc.cfdns.enums.ResultCode;
import com.lwc.cfdns.exception.BadException;
import com.lwc.cfdns.pojo.bo.system.StationBO;
import com.lwc.cfdns.pojo.dto.system.StationDTO;
import com.lwc.cfdns.pojo.entity.SystemStation;
import com.lwc.cfdns.pojo.vo.system.StationVO;
import com.lwc.cfdns.service.SystemStationService;
import com.lwc.cfdns.mapper.SystemStationMapper;
import com.lwc.cfdns.utils.ConvertUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author spring
 * @description 针对表【cfdns_system_station(岗位信息表)】的数据库操作Service实现
 * @createDate 2023-05-26 13:45:01
 */
@Service
public class SystemStationServiceImpl extends ServiceImpl<SystemStationMapper, SystemStation>
        implements SystemStationService {

    /**
     * 创建
     *
     * @param stationDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/26 16:19:02
     */
    @Override
    public Map<String, Object> create(StationDTO stationDTO) {
        Long count = this.baseMapper.selectCount(new LambdaQueryWrapper<SystemStation>().eq(SystemStation::getCode, stationDTO.getCode()));
        if (count > 0) {
            throw new BadException("岗位编码已存在");
        }
        SystemStation systemStation = new SystemStation();
        BeanUtils.copyProperties(stationDTO, systemStation);
        this.baseMapper.insert(systemStation);
        Map<String, Object> map = new HashMap<>();
        map.put("id", systemStation.getId());
        return map;
    }

    /**
     * 详情
     *
     * @param id
     * @return StationVO
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/26 16:23:39
     */
    @Override
    public StationVO view(Long id) {
        SystemStation systemStation = this.baseMapper.selectById(id);
        Assert.notNull(systemStation, ResultCode.NOT_DATA_FOUNT);
        StationVO stationVO = new StationVO();
        BeanUtils.copyProperties(systemStation, stationVO);
        return stationVO;
    }

    /**
     * 修改
     *
     * @param id
     * @param stationDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/26 16:26:08
     */
    @Override
    public Map<String, Object> update(Long id, StationDTO stationDTO) {
        SystemStation systemStation = this.baseMapper.selectById(id);
        Assert.notNull(systemStation, ResultCode.NOT_DATA_FOUNT);
        BeanUtils.copyProperties(stationDTO, systemStation);
        systemStation.setId(id);
        int i = this.baseMapper.updateById(systemStation);
        Map<String, Object> map = new HashMap<>();
        map.put("result", i);
        return map;
    }

    /**
     * 删除
     *
     * @param ids
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/26 16:28:05
     */
    @Override
    public Map<String, Object> delete(String ids) {
        int i = 0;
        if (ids.contains(",")) {
            i = this.baseMapper.delete(new LambdaQueryWrapper<SystemStation>().in(SystemStation::getId, ConvertUtils.toLongArray(ids)));
        } else {
            i = this.baseMapper.deleteById(ConvertUtils.toLong(ids));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("result", i);
        return map;
    }

    /**
     * 列表
     *
     * @param stationBO
     * @return IPage<StationVO>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/26 16:32:59
     */
    @Override
    public IPage<StationVO> lists(StationBO stationBO) {
        Page<SystemStation> page = this.baseMapper.selectPage(stationBO.toPage(), stationBO.toQueryWrapper());
        Page<StationVO> page1 = new Page<>();
        page1.setTotal(page.getTotal());
        page1.setRecords(new ArrayList<StationVO>());
        for (SystemStation systemStation : page.getRecords()) {
            StationVO stationVO = new StationVO();
            BeanUtils.copyProperties(systemStation, stationVO);
            page1.getRecords().add(stationVO);
        }
        return page1;
    }
}




